package com.mew.chess.core.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.mew.common.rest.bo.GameSession;
import lombok.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * @author dylin
 * @create 2021/5/18
 */
public class ChessGameUtil {
    private final static Logger PLAY_INFO = LoggerFactory.getLogger("PLAY_INFO");


    public static int computeTax(long payment, int roomTax) {
        return payment < 1 || roomTax < 1 ? 0 : (int) (payment * roomTax / 100);
    }

    public static long generateINO(long oldIno) {
        return generateINO(1, oldIno);
    }

    public static long generateINO(int tableId,  long oldIno) {
        String md = oldIno < 1 ? DateUtil.format(DateUtil.date(), "MMdd") : getMD(oldIno);
        return null == md ? ++oldIno : Long.parseLong(tableId + md + "0001");

    }

    private static String getMD(long ino){
        if (ino < 100000000){
            return DateUtil.format(DateUtil.date(), "MMdd");
        }
        String inoStr = String.valueOf(ino);
        String omd = StringUtils.substring(inoStr, inoStr.length() -8).substring(0, 4);
        String md = DateUtil.format(DateUtil.date(), "MMdd");
        if (md.equals(omd)) {
            return null;
        }
        return md;
    }


    public static void hpLog(long ino, GameSession gs, long[] bets, long[] payment, int[] winTypes) {
        PLAY_INFO.info(ChessHPLog.builder()
                .gameId(gs.getGameId())
                .vfx(gs.getVfx())
                .roomId(gs.getRoomId())
                .ino(ino)
                .userId(gs.getUserId())
                .bets(bets)
                .payments(payment)
                .winTypes(winTypes)
                .build().toString());
    }

    public static void ccLog(long ino, GameSession gs, long bet, long payment, List<int[]> cards) {
        PLAY_INFO.info(ChessCCLog.builder()
                .gameId(gs.getGameId())
                .vfx(gs.getVfx())
                .roomId(gs.getRoomId())
                .tableId(gs.getTableId())
                .seatNo(gs.getSeatNo())
                .ino(ino)
                .userId(gs.getUserId())
                .bet(bet)
                .payment(payment)
                .cards(cards.toArray(new int[][]{}))
                .toString());
    }

    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    private static class ChessHPLog {
        private int gameId;
        private String vfx;
        private int roomId;
        private long ino;
        private long userId;
        private long[] bets;
        private long[] payments;
        private int[] winTypes;
    }

    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    private static class ChessCCLog {
        private int gameId;
        private String vfx;
        private int roomId;
        private int tableId;
        private int seatNo;
        private long ino;
        private long userId;
        private long bet;
        private long payment;
        private int[][] cards;
    }

    public static void main(String[] args) {
        System.out.println(generateINO(0));
    }

}
